<?php

namespace Frieza\Modules\FeaturePosition\Controllers;

use App\Admin\Extensions\Tools\UserButton;
use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel;
use Doraemon\model\AdmMsgSendDetails;
use Doraemon\model\Member;
use Doraemon\model\Position\PositionRules;
use Doraemon\service\AdmMsgSend as sAdmMsgSend;
use Doraemon\service\BadgeService;
use Doraemon\service\position\AdmOperationModule;
use Doraemon\service\User;
use Doraemon\tools\Log;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Displayers\DropdownActions;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Doraemon\model\Totem\Totems as mTotems;
use Doraemon\service\Totem\TotemService;
use Doraemon\model\Totem\TotemTypes as mTotemTypes;
use Illuminate\Support\MessageBag;
use Frieza\Modules\Totem\Actions\UpdateTotem;
use Frieza\Modules\Totem\Actions\SoldOutTotem;
use Frieza\Modules\Totem\Actions\RackTotem;
use Doraemon\model\FeatureModuleInfo;
use Doraemon\service\BusinessUser\FeaturePositionService;
use Frieza\Modules\FeaturePosition\Actions\DeleteFeature;
use Frieza\Modules\FeaturePosition\Actions\EditFeature;


class FeaturePositionController extends Controller
{
    const PAGE_SIZE = 100;

    public static $button_num = [
        1 => 1,
        2 => 2,
        3 => 3,
        4 => 4,
        5 => 5,
        6 => 6,
        7 => 7,
        8 => 8,
        9 => 9,
        10 => 10,
    ];

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    public function index(Request $request, Content $content)
    {
        $request_data = $request->all();
        $pageSize = $request->input("per_page", self::PAGE_SIZE);
        $page = $request->input("page", 1);
        $limit = [($page - 1) * $pageSize, $pageSize];
        //处理无筛选参数的情况
        $all_module_info = FeatureModuleInfo::getAllModuleInfo($limit);
        if ($request_data){
            //处理有筛选参数的情况
            if ($request_data['_sort']){
                //处理排序
                $where['ORDER'] = [$request_data['_sort']['column'] => strtoupper($request_data['_sort']['type'])];
                $where['LIMIT'] = $limit;
                $all_module_info = FeatureModuleInfo::selectByWhere($where);
            }
        }
        $total = FeatureModuleInfo::count('*');
        $gridModel = new GridDataModel($all_module_info, $total, [
            'perPage' => $pageSize,
        ]);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableFilter();
                $grid->disableRowSelector();
                $grid->disableCreateButton();
                $grid->tools(function (Grid\Tools $tool) {
                    $tool->append(new UserButton(UserButton::TYPE_HREF, '', '新增模块', '', url('/admin/FeaturePosition/create')));
                });
                $grid->setActionClass(DropdownActions::class);
                $grid->perPages([100, 500, 1000, 2000, 5000]);
                $grid->column('id', 'ID')->sortable();
                $grid->column('pos', '位置');
                $grid->column('title', '模块标题');
                $grid->column('remark', '备注信息');
                $grid->column('show_status', '展示状态')->display(function(){
                    $start_time = strtotime($this->start_show_time);
                    $end_time = strtotime($this->end_show_time);
                    if ($this->show_type == FeatureModuleInfo::SHOW_TYPE_FOREVER){
                        $time_str = $this->start_show_time."永久生效";
                        if ($start_time > time()){
                            $show_str = "未生效";
                        }else{
                            $show_str = "正在展示";
                        }
                    }elseif ($this->show_type == FeatureModuleInfo::SHOW_TYPE_TIME){
                        $time_str = "";
                        $show_str = "未生效";
                        if (time() > $start_time && time() <= $end_time){
                            $time_str = $this->start_show_time . "至" . $this->end_show_time;
                            $show_str = "正在展示";
                        }
                    }
                    return "<div><span class='label label-success' style='font-size: 12px'>" . $show_str . "</span></div><br><div><span class='label label-success' style='font-size: 12px'>" . $time_str . "</span></div>";
                });
                $grid->column('card_type', '卡片类型')->display(function(){
                    $card_type =  FeatureModuleInfo::CARD_TYPE_MAP[$this->card_type];
                    return "<div><span class='label label-success' style='font-size: 15px'>" . $card_type . "</span></div><br>";
                });
                $grid->column("admin_id", '操作人员')->display(function () {
                    return "<span style='width:80px;display: inline-block;'>".Admin::user()->find($this->admin_id)->name."</span>";
                });
                $grid->actions(function (Grid\Displayers\Actions $actions){
                    $actions->disableDelete();
                    $actions->disableEdit();
                    $actions->disableView();
                    $id = $actions->row->id;
                    $actions->add(new EditFeature);
                    $actions->add(new DeleteFeature);
                });
            });

        return $content
            ->header('精选列表配置')
            ->description('精选列表信息')
            ->row($gridList);
    }


    /**
     * 新增徽章视图
     * @param Request $request
     * @param Content $content
     * @return Content
     */
    public function create(Request $request, Content $content)
    {
        $header = '新增精选列表模块';
        $row = [];
        $form = new Form($row);
        $tab = new Tab();
        $tab->add($header, $form);

        $form->number("pos", "模块位置");
        $form->text('title', '模块标题')->required();
        $form->text('remark', '备注信息');
        $form->radio('show_type', '展示时间')
            ->options([1 => '指定时间段', 2 => '永久生效'])
            ->when(1, function (Form $form) {
                $form->datetime('start_time', '开始显示时间');
                $form->datetime('end_time', '显示结束时间');
            })->when(2, function (Form $form) {
                $form->datetime('start_time', '开始显示时间');
            })->required();
        $form->userProfile('user_profile', '用户分群')
            ->options(\Doraemon\service\position\UserProfileModule::$other_position_rules)
            ->generate();
        $form->divider();
        $form->radio('card_type', '卡片类型')
            ->options([1001 => 'banner', 1003 => '内容推单人', 1004 => '多人名单'])
            ->default(1001)
            ->when(1001, function (Form $form) {
                $form->radio('is_roll', '是否轮播')
                    ->options([true => '是', false => '否'])
                    ->default(false);
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片1</div>");
                $form->imgUpload('bn_img_url1', '上传图片');
                $form->multiLinkSelect('bn_link1', '跳转链接');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片2</div>");
                $form->imgUpload('bn_img_url2', '上传图片');
                $form->multiLinkSelect('bn_link2', '跳转链接');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片3</div>");
                $form->imgUpload('bn_img_url3', '上传图片');
                $form->multiLinkSelect('bn_link3', '跳转链接');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片4</div>");
                $form->imgUpload('bn_img_url4', '上传图片');
                $form->multiLinkSelect('bn_link4', '跳转链接');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片5</div>");
                $form->imgUpload('bn_img_url5', '上传图片');
                $form->multiLinkSelect('bn_link5', '跳转链接');
            })->when(1003, function (Form $form) {
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片1</div>");
                $form->radio('upload_type1', '内容类型')
                    ->options([1 => '图片', 2 => '视频'])
                    ->default(1)
                    ->when(1, function (Form $form){
                        $form->imgUpload('co_img_url1', '上传图片');
                    })->when(2, function (Form $form){
                        $form->videoUpload('co_video_id1', '上传视频')->help('单个视频上传');
                    });
                $form->multiLinkSelect('co_link1', '跳转链接');
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 15px;\">名片信息</div>");
                $form->text("co_uid1", '所属账号uid');
                $form->text("co_introduce1", '名片介绍文案');
                $form->text("co_button_word1", '名片按钮文案');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片2</div>");
                $form->radio('upload_type2', '内容类型')
                    ->options([1 => '图片', 2 => '视频'])
                    ->default(1)
                    ->when(1, function (Form $form){
                        $form->imgUpload('co_img_url2', '上传图片');
                    })->when(2, function (Form $form){
                        $form->videoUpload('co_video_id2', '上传视频')->help('单个视频上传');
                    });
                $form->multiLinkSelect('co_link2', '跳转链接');
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 15px;\">名片信息</div>");
                $form->text("co_uid2", '所属账号uid');
                $form->text("co_introduce2", '名片介绍文案');
                $form->text("co_button_word2", '名片按钮文案');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片3</div>");
                $form->radio('upload_type3', '内容类型')
                    ->options([1 => '图片', 2 => '视频'])
                    ->default(1)
                    ->when(1, function (Form $form){
                        $form->imgUpload('co_img_url3', '上传图片');
                    })->when(2, function (Form $form){
                        $form->videoUpload('co_video_id3', '上传视频')->help('单个视频上传');
                    });
                $form->multiLinkSelect('co_link3', '跳转链接');
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 15px;\">名片信息</div>");
                $form->text("co_uid3", '所属账号uid');
                $form->text("co_introduce3", '名片介绍文案');
                $form->text("co_button_word3", '名片按钮文案');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片4</div>");
                $form->radio('upload_type4', '内容类型')
                    ->options([1 => '图片', 2 => '视频'])
                    ->default(1)
                    ->when(1, function (Form $form){
                        $form->imgUpload('co_img_url4', '上传图片');
                    })->when(2, function (Form $form){
                        $form->videoUpload('co_video_id4', '上传视频')->help('单个视频上传');
                    });
                $form->multiLinkSelect('co_link4', '跳转链接');
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 15px;\">名片信息</div>");
                $form->text("co_uid4", '所属账号uid');
                $form->text("co_introduce4", '名片介绍文案');
                $form->text("co_button_word4", '名片按钮文案');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片5</div>");
                $form->radio('upload_type5', '内容类型')
                    ->options([1 => '图片', 2 => '视频'])
                    ->default(1)
                    ->when(1, function (Form $form){
                        $form->imgUpload('co_img_url5', '上传图片');
                    })->when(2, function (Form $form){
                        $form->videoUpload('co_video_id5', '上传视频')->help('单个视频上传');
                    });
                $form->multiLinkSelect('co_link5', '跳转链接');
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 15px;\">名片信息</div>");
                $form->text("co_uid5", '所属账号uid');
                $form->text("co_introduce5", '名片介绍文案');
                $form->text("co_button_word5", '名片按钮文案');
            })->when(1004, function (Form $form) {
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片信息</div>");
                $form->imgUpload('mo_img_url', '上传图片');
                $form->text("sub_title", '模块描述文案');
                $form->multiLinkSelect('mo_link', '跳转链接');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">名片<div style=\"font-family: '黑体'; font-size: 15px; color: red\">(必须4个)</div></div>");
                $form->text("mo_uid1", '账号一UID');
                $form->text("mo_introduce1", '介绍文案');
                $form->text("mo_button_word1", '按钮文案');
                $form->color("mo_button_color1","按钮背景色");
                $form->divider();
                $form->text("mo_uid2", '账号二UID');
                $form->text("mo_introduce2", '介绍文案');
                $form->text("mo_button_word2", '按钮文案');
                $form->color("mo_button_color2","按钮背景色");
                $form->divider();
                $form->text("mo_uid3", '账号三uid');
                $form->text("mo_introduce3", '介绍文案');
                $form->text("mo_button_word3", '按钮文案');
                $form->color("mo_button_color3","按钮背景色");
                $form->divider();
                $form->text("mo_uid4", '账号四uid');
                $form->text("mo_introduce4", '介绍文案');
                $form->text("mo_button_word4", '按钮文案');
                $form->color("mo_button_color4","按钮背景色");
            });
        $form->action(url('admin/FeaturePosition/save'));
        return $content
            ->header('新增模块')
            ->description('新增精选列表模块')
            ->breadcrumb(['text' => '新增模块'])
            ->body($tab);
    }

    /**
     * 保存精选列表配置信息
     * @param Request $request
     * @param Content $content
     * @return \Illuminate\Http\RedirectResponse
     */
    public function save(Request $request, Content $content)
    {
        try {
            $params = $request->input();
            unset($params['_token']);
            //dd($params);

            FeaturePositionService::SaveFeaturePositionInfo($params);
            //处理保存的逻辑
            return redirect(url('/admin/FeaturePosition/list'))->with(compact('success'));
        } catch (\Exception $e) {
            Log::debug($e->getMessage());
            $error = new MessageBag([
                'title' => '操作失败,信息:' . $e->getMessage()
            ]);
            return back()->with(compact('error'));
        }
    }


    /**
     * 编辑徽章视图
     * @param Request $request
     * @param Content $content
     * @return Content
     */
    public function edit(Request $request, Content $content,$id)
    {
        $header = '编辑模块信息';
        $row = [];
        //获取徽章信息
        $row = FeatureModuleInfo::getFeatureModuleInfo($id);
        //dd($row);
        //将信息处理一下
        $userProfile = AdmOperationModule::getPositionRulesById($id, PositionRules::MODULE_TYPE_FEATURE_POSITION, true) ?: [];
        $row['user_profile'] = $userProfile;
        $row = FeaturePositionService::FormatEditFeatureInfo($row);

        $form = new Form($row);
        $tab = new Tab();
        $tab->add($header, $form);
        if ($id) {
            $form->hidden('id');
        }
        $form->number("pos", "模块位置");
        $form->text('title', '模块标题')->required();
        $form->text('remark', '备注信息');
        $form->radio('show_type', '展示时间')
            ->options([1 => '指定时间段', 2 => '永久生效'])
            ->when(1, function (Form $form) {
                $form->datetime('start_time', '开始显示时间');
                $form->datetime('end_time', '显示结束时间');
            })->when(2, function (Form $form) {
                $form->datetime('start_time', '开始显示时间');
            });
        $form->userProfile('user_profile', '用户分群')
            ->options(\Doraemon\service\position\UserProfileModule::$other_position_rules)
            ->generate();
        $form->divider();
        $form->radio('card_type', '卡片类型')
            ->options([1001 => 'banner', 1003 => '内容推单人', 1004 => '多人名单'])
            ->default(1001)
            ->when(1001, function (Form $form) {
                $form->radio('is_roll', '是否轮播')
                    ->options([true => '是', false => '否'])
                    ->default(false);
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片1</div>");
                $form->imgUpload('bn_img_url1', '上传图片');
                $form->multiLinkSelect('bn_link1', '跳转链接');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片2</div>");
                $form->imgUpload('bn_img_url2', '上传图片');
                $form->multiLinkSelect('bn_link2', '跳转链接');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片3</div>");
                $form->imgUpload('bn_img_url3', '上传图片');
                $form->multiLinkSelect('bn_link3', '跳转链接');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片4</div>");
                $form->imgUpload('bn_img_url4', '上传图片');
                $form->multiLinkSelect('bn_link4', '跳转链接');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片5</div>");
                $form->imgUpload('bn_img_url5', '上传图片');
                $form->multiLinkSelect('bn_link5', '跳转链接');
            })->when(1003, function (Form $form) {
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片1</div>");
                $form->radio('upload_type1', '内容类型')
                    ->options([1 => '图片', 2 => '视频'])
                    ->default(1)
                    ->when(1, function (Form $form){
                        $form->imgUpload('co_img_url1', '上传图片');
                    })->when(2, function (Form $form){
                        $form->videoUpload('co_video_id1', '上传视频')->help('单个视频上传');
                    });
                $form->multiLinkSelect('co_link1', '跳转链接');
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 15px;\">名片信息</div>");
                $form->text("co_uid1", '所属账号uid');
                $form->text("co_introduce1", '名片介绍文案');
                $form->text("co_button_word1", '名片按钮文案');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片2</div>");
                $form->radio('upload_type2', '内容类型')
                    ->options([1 => '图片', 2 => '视频'])
                    ->default(1)
                    ->when(1, function (Form $form){
                        $form->imgUpload('co_img_url2', '上传图片');
                    })->when(2, function (Form $form){
                        $form->videoUpload('co_video_id2', '上传视频')->help('单个视频上传');
                    });
                $form->multiLinkSelect('co_link2', '跳转链接');
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 15px;\">名片信息</div>");
                $form->text("co_uid2", '所属账号uid');
                $form->text("co_introduce2", '名片介绍文案');
                $form->text("co_button_word2", '名片按钮文案');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片3</div>");
                $form->radio('upload_type3', '内容类型')
                    ->options([1 => '图片', 2 => '视频'])
                    ->default(1)
                    ->when(1, function (Form $form){
                        $form->imgUpload('co_img_url3', '上传图片');
                    })->when(2, function (Form $form){
                        $form->videoUpload('co_video_id3', '上传视频')->help('单个视频上传');
                    });
                $form->multiLinkSelect('co_link3', '跳转链接');
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 15px;\">名片信息</div>");
                $form->text("co_uid3", '所属账号uid');
                $form->text("co_introduce3", '名片介绍文案');
                $form->text("co_button_word3", '名片按钮文案');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片4</div>");
                $form->radio('upload_type4', '内容类型')
                    ->options([1 => '图片', 2 => '视频'])
                    ->default(1)
                    ->when(1, function (Form $form){
                        $form->imgUpload('co_img_url4', '上传图片');
                    })->when(2, function (Form $form){
                        $form->videoUpload('co_video_id4', '上传视频')->help('单个视频上传');
                    });
                $form->multiLinkSelect('co_link4', '跳转链接');
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 15px;\">名片信息</div>");
                $form->text("co_uid4", '所属账号uid');
                $form->text("co_introduce4", '名片介绍文案');
                $form->text("co_button_word4", '名片按钮文案');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片5</div>");
                $form->radio('upload_type5', '内容类型')
                    ->options([1 => '图片', 2 => '视频'])
                    ->default(1)
                    ->when(1, function (Form $form){
                        $form->imgUpload('co_img_url5', '上传图片');
                    })->when(2, function (Form $form){
                        $form->videoUpload('co_video_id5', '上传视频')->help('单个视频上传');
                    });
                $form->multiLinkSelect('co_link5', '跳转链接');
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 15px;\">名片信息</div>");
                $form->text("co_uid5", '所属账号uid');
                $form->text("co_introduce5", '名片介绍文案');
                $form->text("co_button_word5", '名片按钮文案');
            })->when(1004, function (Form $form) {
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">卡片信息</div>");
                $form->imgUpload('mo_img_url', '上传图片');
                $form->text("sub_title", '模块描述文案');
                $form->multiLinkSelect('mo_link', '跳转链接');
                $form->divider();
                $form->html("<div style=\"font-family: '黑体'; font-weight: bold; font-size: 25px;\">名片<div style=\"font-family: '黑体'; font-size: 15px; color: red\">(必须4个)</div></div>");
                $form->text("mo_uid1", '账号一UID');
                $form->text("mo_introduce1", '介绍文案');
                $form->text("mo_button_word1", '按钮文案');
                $form->color("mo_button_color1","按钮背景色");
                $form->divider();
                $form->text("mo_uid2", '账号二UID');
                $form->text("mo_introduce2", '介绍文案');
                $form->text("mo_button_word2", '按钮文案');
                $form->color("mo_button_color2","按钮背景色");
                $form->divider();
                $form->text("mo_uid3", '账号三uid');
                $form->text("mo_introduce3", '介绍文案');
                $form->text("mo_button_word3", '按钮文案');
                $form->color("mo_button_color3","按钮背景色");
                $form->divider();
                $form->text("mo_uid4", '账号四uid');
                $form->text("mo_introduce4", '介绍文案');
                $form->text("mo_button_word4", '按钮文案');
                $form->color("mo_button_color4","按钮背景色");
            });

        $form->action(url('admin/FeaturePosition/save'));
        return $content
            ->header('编辑精选模块信息')
            ->description('编辑模块')
            ->breadcrumb(['text' => '编辑模块信息'])
            ->body($tab);
    }

}
